package com.mb.app.controller;


import com.mb.app.domain.TbAppInfo;
import com.mb.app.domain.TbReportReg;
import com.mb.app.service.ITbAppInfoService;
import com.mb.app.service.ITbReportRegService;
import com.mb.app.service.ITbReportRegService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwl
 * @program
 * @description 总流量统计
 * @date 2019-03-26 11:33
 **/
@Controller
@RequestMapping("/app/reportReg")
@Slf4j
public class ReportRegController extends BaseController {
    

    @Autowired
    private ITbReportRegService reportRegService;
    
    @Autowired
    private ITbAppInfoService appInfoService;

    @GetMapping("/index")
    String index(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/reportReg/reportReg_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String reportDate = (String) params.get("reportDate");
        if (StringUtils.isNotEmpty(reportDate)) {
            params.put("startTime",reportDate.substring(0,10));
            params.put("endTime",reportDate.substring(13,23));
        }
        Query query = new Query(params);
        List<TbReportReg> list = reportRegService.queryList(query);
        int total = reportRegService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }


    @ResponseBody
    @RequestMapping(value = "/exportExcel")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {

        String reportDate = (String) params.get("reportDate");
        if (StringUtils.isNotEmpty(reportDate)) {
            params.put("startTime", reportDate.substring(0, 10));
            params.put("endTime", reportDate.substring(13, 23));
        }
        reportRegService.exprot(response,params);
    }
}
